🎯 Learning Objectives
🎯 We are learning to iterate through the items in a 1 dimensional list
🎯 We are learning how to access items from a record
Outcomes
- We will be able to iterate through the items of a 1 dimensional list
- We will be able to describe the relationship between i and the list item output
- We will be able to output individual items from a record
🐍 Activity 1 – Iterating through a list (recap)
🔎 You have been given a list
Each item in the list is an important character from the novel Frankenstein
frank = ["Dr Frankenstein", "The Creature", "Elizabeth L", "Henry C", "Robert W"]
🐍 Coding (1): Create a for loop
Create a for loop where:
- i starts off equal to index number of the first item in the list (0)
- The loop ends when i is the the same as the length of the list (5)
Click to see how this should look
for i in range(0, 5):
🐍 Coding 2: Creating the output
Create an output (use the print() function)
- The output should contain a list item e.g. listname[0]
- Except, the number is replaced with the loop counter variable (i)
- It should also contain a meaningful message e.g. "is an important character in Frankenstein"
- Make sure you indent any line of code inside the for loop
Click to see how this should look
for i in range(0, 5):
print(f"{frank[i]} is an important character in Frankenstein")
▶️ Runing the code
Run the code and you should see this output:
Dr Frankenstein is an important character in Frankenstein The Creature is an important character in Frankenstein Elizabeth L is an important character in Frankenstein Henry C is an important character in Frankenstein Robert W is an important character in Frankenstein
🐍 Activity 2 – Iterating through a list independently
🐍 Iterate through a list (independent practice)
You have been given a list of items Sue bought from the green grocer
bought = ["🍎 Apple", "🍐 Pear", "🍊 Orange", "🥕 Carrot", "🥬 Lettuce", "🧅 Onion"]
You must write some code that:
- Iterates through each item in the list
- Outputs "A delicious [[[Name of the item]]]"
✍️ Activity 3 – Answer some questions
✍️ Answer some questions
- You have been given some code to examine
- Beneath it are some questions
- Answer questions 3 and 4 with as much detail as you can
🐍 Activity 4 – Output items from a record
🔎 A list made of records
You have been given a 2 dimensional list
This is made of 4 records (smaller lists inside a larger list)
Each record contains two items [Name of the historical figure, their role in the norman conquest]
p1066 = [
["Edward the Confessor", "King of England who died in 1066"],
["Harold Godwinson", "Anglo-Saxon king of England"],
["William of Normandy", "Norman leader who invaded England"],
["Harald Hardrada", "Viking king of Norway"]
]
🐍 Coding (1): Output the entire first record
Write the following code
print("This is the entire first record")
print(p1066[0])
Run it ▶️
You should see this:
This is the entire first record ['Edward the Confessor', 'King of England who died in 1066']
🐍 Coding (2): Output the first item of the third record
Write the following code
print("This is the first item of the third record")
print(p1066[2][0])
Run it ▶️
You should see this:
This is the first item of the third record William of Normandy
🐍 Coding (3): Output the second item of the third record
Write the following code
print("This is the second item of the third record")
print(p1066[2][1])
Run it ▶️
You should see this:
This is the second item of the third record Norman leader who invaded England
🐍 Coding (4): Output the first and second items of the third record together as a meaningful output.
Write the following code
print("This outputs the contents of the third record as a meaningful statement")
print(f"{p1066[2][0]} was the {p1066[2][1]}")
Run it ▶️
You should see this:
This outputs the contents of the third record as a meaningful statement William of Normandy was the Norman leader who invaded England
🐍 Coding (5): Complete the independent tasks
Complete the indepenent tasks
Use what you have just learned
#--------------------- Independent Tasks ----------------------
#====> Output the entire fourth record
#====> Output the first and second items of the fourth record together
#====> as a meaningful output.
#====> Output the first and second items of the first record together
#====> as a meaningful output.
✨ Extension - Iterating through a 2 dimensional list
Extension Activity
You have been given a 2 dimensional list
animals = [
["Lion", "Mammal"],
["Elephant", "Mammal"],
["Gecko", "Reptile"],
["Snake", "Reptile"],
["Salmon", "Fish"],
["Shark", "Fish"],
["Heron", "Bird"],
["Eagle", "Bird"],
["Wasp", "Insect"],
["Ant", "Insect"]
]
You must write code that:
- Iterates through each record
- Outputs the first item and second item of each record
- Accompanies both items with a meaningful message
Try to work it out yourself, but here is the code if you want to see
Click to see the completed code
for i in range(0, 10):
print(f"{animals[i][0]} is a type of {animals[i][1]}")